﻿using Microsoft.AspNetCore.Mvc;
using brand.cyt369.com.MODELS;
using brand.cyt369.com.models;

namespace brand.cyt369.com.Controllers
{
    public class AccountController : BaseController
    {
        private brand.cyt369.com.models.Identity.SignInManager m_SignInManager;
        private brand.cyt369.com.models.Identity.UserManager m_UserManager;

        public AccountController(brand.cyt369.com.models.Identity.UserManager userManager, brand.cyt369.com.models.Identity.SignInManager signInManager)
        {
            m_UserManager = userManager;
            m_SignInManager = signInManager;
        }

        public IActionResult Register()
        {
            return Redirect($"{brand.cyt369.com.utility.SystemSettingService.Instance.UserCenterRegisterURL}?appId={brand.cyt369.com.utility.SystemSettingService.Instance.AppID}");
        }

        public IActionResult Login(string returnUrl, bool? force)
        {
            if (force == true)
            {
                return Redirect($"{brand.cyt369.com.utility.SystemSettingService.Instance.UserCenterLoginURL}?appId={brand.cyt369.com.utility.SystemSettingService.Instance.AppID}&returnUrl={brand.cyt369.com.utility.SystemSettingService.Instance.LoginReturnURL}&resultUrl={returnUrl}&force=true");
            }
            else
            {
                if (this.User.Identity.IsAuthenticated && !string.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return Redirect($"{brand.cyt369.com.utility.SystemSettingService.Instance.UserCenterLoginURL}?appId={brand.cyt369.com.utility.SystemSettingService.Instance.AppID}&returnUrl={brand.cyt369.com.utility.SystemSettingService.Instance.LoginReturnURL}&resultUrl={returnUrl}");
                }
            }
        }

        public async Task<IActionResult> PostLogin(string ticket, string resultUrl)
        {
            if (string.IsNullOrEmpty(resultUrl))
            {
                resultUrl = "~/Home/Index";
            }

            var result = await m_SignInManager.PasswordSignInAsync(ticket, brand.cyt369.com.utility.SystemSettingService.Instance.AppID, false, false);
            if (result.Succeeded)
            {
                var loginUser = await m_UserManager.FindByNameAsync(ticket);
                if (loginUser != null)
                {
                    var userJson = System.Text.Json.JsonSerializer.Serialize(loginUser);
                    //this.HttpContext.Session.Set(brand.cyt369.com.utility.ConstStringClass.SessionUser, System.Text.Encoding.UTF8.GetBytes(userJson));
                }

                ViewBag.ReturnUrl = Url.Content(resultUrl);
                return View();
            }
            else
            {
                return Redirect(resultUrl);
            }
        }

        [HttpGet]
        public virtual async Task<IActionResult> Logout()
        {
            try
            {
                //this.UserSession.Clear();
            }
            catch { }

            try
            {
                await this.m_SignInManager.SignOutAsync();
            }
            catch { }

            return Redirect("~/Home/Index");
        }

    }
}
